﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Ormon.HostLinkLib.FinsBase
{
    public class FinsEndCode
    {

        /// <summary>
        /// 对比回复帧中的EndCode，看内容是否相符
        /// </summary>
        /// <param name="ResponseFrame">回复帧</param>
        /// <param name="EndCodeAdr">结束代码在回复帧的起始位置</param>
        /// <returns>返回结束代码字符串</returns>
        public static string CatchEndCode(List<byte> ResponseFrame, int EndCodeAdr)
        {
            try
            {
                List<byte> ResponseEndCode = ResponseFrame.GetRange(EndCodeAdr, 4);
                string EndCodeContents = null;
                

                if (ResponseEndCode.SequenceEqual(new List<byte> { 0x30, 0x30, 0x30, 0x30 }))
                {
                    EndCodeContents = "0000";
                }
                else
                {
                    foreach (var EndCodeMap in EndCodeMapSeq)
                    {
                        if (ResponseEndCode.SequenceEqual(EndCodeMap.Key))
                        {
                            EndCodeContents = EndCodeMap.Value;
                            break;
                        }
                    }
                }
                return EndCodeContents;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private static readonly Dictionary<List<byte>, string> EndCodeMapSeq = new Dictionary<List<byte>, string>
        {
            {new List<byte>{ 0x30, 0x30, 0x30, 0x31 },"MainCode: 00, SubCode: 01;  Contents: Service canceled" },

            {new List<byte>{ 0x30, 0x31, 0x30, 0x31 },"MainCode: 01, SubCode: 01;  Contents: Local node not in network" },
            {new List<byte>{ 0x30, 0x31, 0x30, 0x32 },"MainCode: 01, SubCode: 02;  Contents: Token timeout" },
            {new List<byte>{ 0x30, 0x31, 0x30, 0x33 },"MainCode: 01, SubCode: 03;  Contents: Retries failed" },
            {new List<byte>{ 0x30, 0x31, 0x30, 0x34 },"MainCode: 01, SubCode: 04;  Contents: Too many send frames" },
            {new List<byte>{ 0x30, 0x31, 0x30, 0x35 },"MainCode: 01, SubCode: 05;  Contents: Node address range error" },
            {new List<byte>{ 0x30, 0x31, 0x30, 0x36 },"MainCode: 01, SubCode: 06;  Contents: Node address duplication" },

            {new List<byte>{ 0x30, 0x32, 0x30, 0x31 },"MainCode: 02, SubCode: 01;  Contents: Destination node not in network" },
            {new List<byte>{ 0x30, 0x32, 0x30, 0x32 },"MainCode: 02, SubCode: 02;  Contents: Unit missing" },
            {new List<byte>{ 0x30, 0x32, 0x30, 0x33 },"MainCode: 02, SubCode: 03;  Contents: Third node missing" },
            {new List<byte>{ 0x30, 0x32, 0x30, 0x34 },"MainCode: 02, SubCode: 04;  Contents: Destination node busy" },
            {new List<byte>{ 0x30, 0x32, 0x30, 0x35 },"MainCode: 02, SubCode: 05;  Contents: Response timeout" },

            {new List<byte>{ 0x30, 0x33, 0x30, 0x31 },"MainCode: 03, SubCode: 01;  Contents: Communications controller error" },
            {new List<byte>{ 0x30, 0x33, 0x30, 0x32 },"MainCode: 03, SubCode: 02;  Contents: CPU Unit error" },
            {new List<byte>{ 0x30, 0x33, 0x30, 0x33 },"MainCode: 03, SubCode: 03;  Contents: Controller error" },
            {new List<byte>{ 0x30, 0x33, 0x30, 0x34 },"MainCode: 03, SubCode: 04;  Contents: Unit number error" },

            {new List<byte>{ 0x30, 0x34, 0x30, 0x31 },"MainCode: 04, SubCode: 01;  Contents: Undefined command" },
            {new List<byte>{ 0x30, 0x34, 0x30, 0x32 },"MainCode: 04, SubCode: 02;  Contents: Not supported by model/version" },

            {new List<byte>{ 0x30, 0x35, 0x30, 0x31 },"MainCode: 05, SubCode: 01;  Contents: Destination address setting error" },
            {new List<byte>{ 0x30, 0x35, 0x30, 0x32 },"MainCode: 05, SubCode: 02;  Contents: No routing tables" },
            {new List<byte>{ 0x30, 0x35, 0x30, 0x33 },"MainCode: 05, SubCode: 03;  Contents: Routing table error" },
            {new List<byte>{ 0x30, 0x35, 0x30, 0x34 },"MainCode: 05, SubCode: 04;  Contents: Too many relays" },

            {new List<byte>{ 0x31, 0x30, 0x30, 0x31 },"MainCode: 10, SubCode: 01;  Contents: Command too long" },
            {new List<byte>{ 0x31, 0x30, 0x30, 0x32 },"MainCode: 10, SubCode: 02;  Contents: Command too short" },
            {new List<byte>{ 0x31, 0x30, 0x30, 0x33 },"MainCode: 10, SubCode: 03;  Contents: Elements/data don’t match" },
            {new List<byte>{ 0x31, 0x30, 0x30, 0x34 },"MainCode: 10, SubCode: 04;  Contents: Command format error" },
            {new List<byte>{ 0x31, 0x30, 0x30, 0x35 },"MainCode: 10, SubCode: 05;  Contents: Header error" },

            {new List<byte>{ 0x31, 0x31, 0x30, 0x31 },"MainCode: 11, SubCode: 01;  Contents: Area classification missing" },
            {new List<byte>{ 0x31, 0x31, 0x30, 0x32 },"MainCode: 11, SubCode: 02;  Contents: Access size error" },
            {new List<byte>{ 0x31, 0x31, 0x30, 0x33 },"MainCode: 11, SubCode: 03;  Contents: Address range error" },
            {new List<byte>{ 0x31, 0x31, 0x30, 0x34 },"MainCode: 11, SubCode: 04;  Contents: Address range exceeded" },
            {new List<byte>{ 0x31, 0x31, 0x30, 0x36 },"MainCode: 11, SubCode: 06;  Contents: Program missing" },
            {new List<byte>{ 0x31, 0x31, 0x30, 0x39 },"MainCode: 11, SubCode: 09;  Contents: Relational error" },
            {new List<byte>{ 0x31, 0x31, 0x30, 0x41 },"MainCode: 11, SubCode: 0A;  Contents: Duplicate data access" },
            {new List<byte>{ 0x31, 0x31, 0x30, 0x42 },"MainCode: 11, SubCode: 0B;  Contents: Response too long" },
            {new List<byte>{ 0x31, 0x31, 0x30, 0x43 },"MainCode: 11, SubCode: 0C;  Contents: Parameter error" },

            {new List<byte>{ 0x32, 0x30, 0x30, 0x32 },"MainCode: 20, SubCode: 02;  Contents: Protected" },
            {new List<byte>{ 0x32, 0x30, 0x30, 0x33 },"MainCode: 20, SubCode: 03;  Contents: Table missing" },
            {new List<byte>{ 0x32, 0x30, 0x30, 0x34 },"MainCode: 20, SubCode: 04;  Contents: Data missing" },
            {new List<byte>{ 0x32, 0x30, 0x30, 0x35 },"MainCode: 20, SubCode: 05;  Contents: Program missing" },
            {new List<byte>{ 0x32, 0x30, 0x30, 0x36 },"MainCode: 20, SubCode: 06;  Contents: File missing" },
            {new List<byte>{ 0x32, 0x30, 0x30, 0x37 },"MainCode: 20, SubCode: 07;  Contents: Data mismatch" },

            {new List<byte>{ 0x32, 0x31, 0x30, 0x31 },"MainCode: 21, SubCode: 01;  Contents: Read only" },
            {new List<byte>{ 0x32, 0x31, 0x30, 0x32 },"MainCode: 21, SubCode: 02;  Contents: Protected Cannot write data link table" },
            {new List<byte>{ 0x32, 0x31, 0x30, 0x33 },"MainCode: 21, SubCode: 03;  Contents: Cannot register" },
            {new List<byte>{ 0x32, 0x31, 0x30, 0x35 },"MainCode: 21, SubCode: 05;  Contents: Program missing" },
            {new List<byte>{ 0x32, 0x31, 0x30, 0x36 },"MainCode: 21, SubCode: 06;  Contents: File missing" },
            {new List<byte>{ 0x32, 0x31, 0x30, 0x37 },"MainCode: 21, SubCode: 07;  Contents: File name already exists" },
            {new List<byte>{ 0x32, 0x31, 0x30, 0x38 },"MainCode: 21, SubCode: 08;  Contents: Cannot change" },

            {new List<byte>{ 0x32, 0x32, 0x30, 0x31 },"MainCode: 22, SubCode: 01;  Contents: Not possible during execution" },
            {new List<byte>{ 0x32, 0x32, 0x30, 0x32 },"MainCode: 22, SubCode: 02;  Contents: Not possible while running" },
            {new List<byte>{ 0x32, 0x32, 0x30, 0x33 },"MainCode: 22, SubCode: 03;  Contents: Wrong PLC mode" },
            {new List<byte>{ 0x32, 0x32, 0x30, 0x34 },"MainCode: 22, SubCode: 04;  Contents: Wrong PLC mode" },
            {new List<byte>{ 0x32, 0x32, 0x30, 0x35 },"MainCode: 22, SubCode: 05;  Contents: Wrong PLC mode" },
            {new List<byte>{ 0x32, 0x32, 0x30, 0x36 },"MainCode: 22, SubCode: 06;  Contents: Wrong PLC mode" },
            {new List<byte>{ 0x32, 0x32, 0x30, 0x37 },"MainCode: 22, SubCode: 07;  Contents: Specified node not polling node" },
            {new List<byte>{ 0x32, 0x32, 0x30, 0x38 },"MainCode: 22, SubCode: 08;  Contents: Step cannot be executed" },

            {new List<byte>{ 0x32, 0x33, 0x30, 0x31 },"MainCode: 23, SubCode: 01;  Contents: File device missing" },
            {new List<byte>{ 0x32, 0x33, 0x30, 0x32 },"MainCode: 23, SubCode: 02;  Contents: Memory missing" },
            {new List<byte>{ 0x32, 0x33, 0x30, 0x33 },"MainCode: 23, SubCode: 03;  Contents: Clock missing" },

            {new List<byte>{ 0x32, 0x34, 0x30, 0x31 },"MainCode: 24, SubCode: 01;  Contents: Table missing" },

            {new List<byte>{ 0x32, 0x35, 0x30, 0x32 },"MainCode: 25, SubCode: 02;  Contents: Memory error" },
            {new List<byte>{ 0x32, 0x35, 0x30, 0x33 },"MainCode: 25, SubCode: 03;  Contents: I/O setting error" },
            {new List<byte>{ 0x32, 0x35, 0x30, 0x34 },"MainCode: 25, SubCode: 04;  Contents: Too many I/O points" },
            {new List<byte>{ 0x32, 0x35, 0x30, 0x35 },"MainCode: 25, SubCode: 05;  Contents: CPU bus error" },
            {new List<byte>{ 0x32, 0x35, 0x30, 0x36 },"MainCode: 25, SubCode: 06;  Contents: I/O duplication" },
            {new List<byte>{ 0x32, 0x35, 0x30, 0x37 },"MainCode: 25, SubCode: 07;  Contents: I/O bus error" },
            {new List<byte>{ 0x32, 0x35, 0x30, 0x39 },"MainCode: 25, SubCode: 09;  Contents: SYSMAC BUS/2 error" },
            {new List<byte>{ 0x32, 0x35, 0x30, 0x41 },"MainCode: 25, SubCode: 0A;  Contents: CPU Bus Unit error" },
            {new List<byte>{ 0x32, 0x35, 0x30, 0x44 },"MainCode: 25, SubCode: 0D;  Contents: SYSMAC BUS No. duplication" },
            {new List<byte>{ 0x32, 0x35, 0x30, 0x46 },"MainCode: 25, SubCode: 0F;  Contents: Memory error" },
            {new List<byte>{ 0x32, 0x35, 0x31, 0x30 },"MainCode: 25, SubCode: 10;  Contents: SYSMAC BUS terminator missing" },

            {new List<byte>{ 0x32, 0x36, 0x30, 0x31 },"MainCode: 26, SubCode: 01;  Contents: No protection" },
            {new List<byte>{ 0x32, 0x36, 0x30, 0x32 },"MainCode: 26, SubCode: 02;  Contents: Incorrect password" },
            {new List<byte>{ 0x32, 0x36, 0x30, 0x34 },"MainCode: 26, SubCode: 04;  Contents: Protected" },
            {new List<byte>{ 0x32, 0x36, 0x30, 0x35 },"MainCode: 26, SubCode: 05;  Contents: Service already executing" },
            {new List<byte>{ 0x32, 0x36, 0x30, 0x36 },"MainCode: 26, SubCode: 06;  Contents: Service stopped" },
            {new List<byte>{ 0x32, 0x36, 0x30, 0x37 },"MainCode: 26, SubCode: 07;  Contents: No execution right" },
            {new List<byte>{ 0x32, 0x36, 0x30, 0x38 },"MainCode: 26, SubCode: 08;  Contents: Settings not complete" },
            {new List<byte>{ 0x32, 0x36, 0x30, 0x39 },"MainCode: 26, SubCode: 09;  Contents: Necessary items not set" },
            {new List<byte>{ 0x32, 0x36, 0x30, 0x41 },"MainCode: 26, SubCode: 0A;  Contents: Number already defined" },
            {new List<byte>{ 0x32, 0x36, 0x30, 0x42 },"MainCode: 26, SubCode: 0B;  Contents: Error will not clear" },

            {new List<byte>{ 0x33, 0x30, 0x30, 0x31 },"MainCode: 30, SubCode: 01;  Contents: No access right" },

            {new List<byte>{ 0x34, 0x30, 0x30, 0x31 },"MainCode: 40, SubCode: 01;  Contents: Service aborted" },
        };
    }
}
